#include "ControlManager.hpp"

ControlManager::ControlManager() {
    for (int i = 0; i < 256; ++i) {
        this->keyState[i] = false;
    }

    for (int i = 0; i < 8; ++i) {
        this->mouseButtonState[i] = false;
    }

    for (int i = 0; i < 256; ++i) {
        this->specialKeyState[i] = false;
    }
}

bool ControlManager::doAction() {
    if (this->keyState['E']) {
        this->keyState['E'] = false;
        return true;
    }

    return false;
}

bool ControlManager::goDown() {
    return this->keyState['S'];
}

bool ControlManager::goLeft() {
    return this->keyState['A'];
}

bool ControlManager::goRight() {
    return this->keyState['D'];
}

bool ControlManager::goUp() {
    return this->keyState['W'];
}

bool ControlManager::jump() {
    if (this->keyState[' ']) {
        this->keyState[' '] = false;
        return true;
    }

    return false;
}

void ControlManager::keyDown(unsigned char key) {
    this->keyState[toupper(key)] = true;
}

void ControlManager::keyUp(unsigned char key) {
    this->keyState[toupper(key)] = false;
}

bool ControlManager::menuNext() {
    if (this->specialKeyState[103]) {
        this->specialKeyState[103] = false;
        return true;
    }

    return false;
}

bool ControlManager::menuPrevious() {
    if (this->specialKeyState[101]) {
        this->specialKeyState[101] = false;
        return true;
    }

    return false;
}

bool ControlManager::menuSelect() {
    if (this->keyState[13]) {
        this->keyState[13] = false;
        return true;
    }

    return false;
}

void ControlManager::mouseButtonDown(int button) {
    this->mouseButtonState[button] = true;
}

void ControlManager::mouseButtonUp(int button) {
    if (button != 3 && button != 4) {
        this->mouseButtonState[button] = false;
    }
}

bool ControlManager::openMenu() {
    if (this->keyState[27]) {
        this->keyState[27] = false;
        return true;
    }

    return false;
}

void ControlManager::specialKeyDown(int key) {
    this->specialKeyState[key] = true;
}

void ControlManager::specialKeyUp(int key) {
    this->specialKeyState[key] = false;
}

bool ControlManager::zoomIn() {
    bool zoomIn = this->mouseButtonState[3];
    this->mouseButtonState[3] = false;
    return zoomIn;
}

bool ControlManager::zoomOut() {
    bool zoomOut = this->mouseButtonState[4];
    this->mouseButtonState[4] = false;
    return zoomOut;
}
